<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

class CreateSalesTable extends Migration
{
	private $tableName = 'sales';
	private $tableComment = '分销人员表';
	private $primaryKey = 'id';

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::create($this->tableName, function (Blueprint $table) {
			$table->increments(env('DB_CONNECTION') == 'oracle' ? strtoupper($this->primaryKey) : $this->primaryKey)->comment('众筹id');
			$table->string('sale_openid',50)->comment('众筹者openid')->nullable();
			$table->unsignedInteger('status')->comment('状态1：正常2：删除，0下架')->default(1);
			$table->unsignedInteger('uid')->comment('众筹者id')->default(0);
			$table->Integer('money')->comment('佣金金额')->default(0);
			$table->string('nickname',10)->comment('众筹者昵称')->nullable();
			$table->string('avatar',200)->comment('众筹者头像')->nullable();
			$table->string('invitor1',500)->comment('一级分销信息')->nullable();
			
			$table->string('invitor2',500)->comment('二级分销信息')->nullable();
            $table->timestamps();

			if (env('DB_CONNECTION') == 'oracle') {
				$table->comment = $this->tableComment;
			}
		});

		if (env('DB_CONNECTION') == 'mysql') {
			DB::statement("ALTER TABLE `" . DB::getTablePrefix() . $this->tableName . "` comment '{$this->tableComment}'");
		}
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::dropIfExists($this->tableName);
		if (env('DB_CONNECTION') == 'oracle') {
			$sequence = DB::getSequence();
			$sequence->drop(strtoupper($this->tableName . '_' . $this->primaryKey . '_SEQ'));
		}
	}
}
